package com.xzd.utils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * 参数非空校验工具类
 *
 * @author xuzhengdong 
 *
 */
public class ParamValidator {
	/**
	 * 默认构造方法
	 */
	public ParamValidator() {

	}

	/**
	 * validate param
	 *
	 * @param param
	 *            only one param
	 * @return when Map,Collection,Arrays,String is null or size is 0 ,return
	 *         false; when objects is null,return false;
	 */
	private static boolean validatorParamNotEmpty(Object param) {

		if (null == param) {
			return false;
		}

		/**
		 * validate String
		 */
		if (String.class.isInstance(param)) {
			if(StringUtils.isBlank((String) param)){
				return false;
			}
			if (0 == ((String) param).trim().length()) {
				return false;
			}
		}

		/**
		 * validate Collection
		 */
		if (Collection.class.isInstance(param)) {
			if (0 == ((Collection<?>) param).size()) {
				return false;
			}
		}

		/**
		 * validate Map
		 */
		if (param instanceof Map<?, ?>) {
			if (0 == ((Map<?, ?>) param).size()) {
				return false;
			}
		}

		/**
		 * validate Arrays
		 */
		if (param.getClass().isArray()) {
			if (0 == Array.getLength(param)) {
				return false;
			}
		}

		return true;
	}

	/**
	 * this function validate Map,Collection,Arrays,String,and other objects
	 * except Primitive types is not empty
	 *
	 * @param params
	 *            variable params,accept objects which extends Object
	 * @return when Map,Collection,Arrays,String is null or size is 0 ,return
	 *         false; when objects is null,return false;
	 */
	public static boolean validatorParamsNotEmpty(Object... params) {
		if (null == params) {
			return false;
		}
		for (Object obj : params) {
			if (!ParamValidator.validatorParamNotEmpty(obj)) {
				return false;
			}
		}
		return true;
	}

	/**
	 *
	 * @param params
	 *            variable params,accept objects which extends Object
	 * @return return true when Map,Collection,Arrays,String is null or size is
	 *         0 and objects is null,return true;
	 */
	public static boolean validatorParamsIsEmpty(Object... params) {
		if (null == params) {
			return true;
		}
		for (Object obj : params) {
			if (!ParamValidator.validatorParamNotEmpty(obj)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// String s1 = null;
		// String s2 = "";
		String s3 = null;
		System.out.println(validatorParamsIsEmpty(s3));
		// boolean bl1 = ParamValidator.validatorParam(s1);
		// System.out.println("bl1 = " + bl1);
		// boolean bl2 = ParamValidator.validatorParam(s2);
		// System.out.println("bl2 = " + bl2);
		// boolean bl3 = ParamValidator.validatorParam(s3);
		// System.out.println("bl3 = " + bl3);

		// List ls1 = null;
		// List ls2 = new ArrayList();
		// List ls3 = new ArrayList();
		// ls3.add(s3);

		// boolean bl1 = ParamValidator.validatorParam(ls1);
		// System.out.println("bl1 = " + bl1);
		// boolean bl2 = ParamValidator.validatorParam(ls2);
		// System.out.println("bl2 = " + bl2);
		// boolean bl3 = ParamValidator.validatorParam(ls3);
		// System.out.println("bl3 = " + bl3);

		// Map map1 = null;
		// Map map2 = new HashMap();
		// Map map3 = new HashMap();
		// map3.put(s3, s3);
		//
		// boolean bl1 = ParamValidator.validatorParam(map1);
		// System.out.println("bl1 = " + bl1);
		// boolean bl2 = ParamValidator.validatorParam(map2);
		// System.out.println("bl2 = " + bl2);
		// boolean bl3 = ParamValidator.validatorParam(map3);
		// System.out.println("bl3 = " + bl3);

//		String b1 = null;
//		String[] b2 = new String[3];
//		int[] b3 = new int[] { 1, 2 };
//		Map<String, String> map3 = new HashMap<String, String>();
//		map3.put(s3, s3);
//		List<String> ls3 = new ArrayList<String>();
//		ls3.add(s3);
//
//		// int ilen1 = Array.getLength(b2);
//		// System.out.println("ilen1 = " + ilen1);
//		// int ilen2 = Array.getLength(b3);
//		// System.out.println("ilen2 = " + ilen2);
//		// int ilen3 = Array.getLength(b4);
//		// System.out.println("ilen3 = " + ilen3);
//
//		boolean bl1 = ParamValidator.validatorParamsNotEmpty(b1, b2, b3, map3,
//				ls3);
//		System.out.println("bl1 = " + bl1);
//		// boolean bl2 = ParamValidator.validatorParam(b2);
//		// System.out.println("bl2 = " + bl2);
//		// boolean bl3 = ParamValidator.validatorParam(b3);
//		// System.out.println("bl3 = " + bl3);
//
//		// String[] param = new String[2];
//		// boolean blinstance = param.getClass().isArray();
//		// System.out.println("blinstance = " + blinstance);
//		//
//		// int[] param2 = new int[2];
//		// boolean blinstance2 = param.getClass().isArray();
//		// System.out.println("blinstance2 = " + blinstance2);

	}

}
